//
//  SafeArray.m
//  GCDDemo
//
//  Created by everyu on 2018/6/6.
//  Copyright © 2018年 everyu. All rights reserved.
//

#import "SafeArray.h"
#import "SemaphoreLock.h"

@interface SafeArray (){
    SemaphoreLock *_lock;
    NSMutableArray *_array;
}

@end

@implementation SafeArray
- (instancetype)init
{
    self = [super init];
    if (self) {
        _lock = [[SemaphoreLock alloc] init];
        _array = @[].mutableCopy;
    }
    return self;
}

- (void)addObject:(id)anObject
{
    [_lock lock];
    [_array addObject:anObject];
    [_lock unlock];
}

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
{
    [_lock lock];
    [_array insertObject:anObject atIndex:index];
    [_lock unlock];
}

- (void)removeObjectAtIndex:(NSUInteger)index
{
    [_lock lock];
    [_array removeObjectAtIndex:index];
    [_lock unlock];
}
@end
